import 'dart:convert';
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import 'package:robot3/pojo.dart';
import 'package:robot3/services.dart';
import 'package:robot3/viewmodel/CommodityViewModel.dart';
import 'package:robot3/viewmodel/CommodityViewModel2.dart';
import 'package:robot3/viewmodel/ShangpinkuProvider.dart';
import 'dart:ui';

import 'package:robot3/widgets.dart';

import 'Robot.dart'; // For devicePixelRatio

//  商品库详情
void main() {
  runApp(const Shangpinku());
}

class Shangpinku extends StatelessWidget {
  const Shangpinku({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // return _ShangpinkuScreen(title: 'Flutter Demo Home Page');
    return ChangeNotifierProvider(
      create: (_) => ShangpinkuProvider(),
      child: _ShangpinkuScreen(title: 'Flutter Demo Home Page'),
    );
    ;
  }
}

class _ShangpinkuScreen extends StatefulWidget {
  const _ShangpinkuScreen({super.key, required this.title});

  final String title;

  @override
  State<_ShangpinkuScreen> createState() => _PageState();
}

class _PageState extends State<_ShangpinkuScreen> {
  static const platform = MethodChannel('com.abc.robot01.xiangqing');
  String dataShared = 'No data';

  // String  accessToken="ed8a4b93-873d-42eb-85e7-19bf74cbb053";
  String id = "1bdc4e2b35764b46b133481f0807e5d6";
  String? name = "";

  // Commodity? commodity;  // 声明 Commodity 类型的变量
  @override
  void initState() {
    super.initState();
    setState(() {
      dataShared = "商品库";
    });

    final provider = Provider.of<ShangpinkuProvider>(context, listen: false);
    provider.login().then((value) {
      if (value == 1) {
        provider.getCompanyProduct();
      }
    });
    // getId();
    //
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // Image equivalent to the Image composable
          Image.asset(
            'images/bg.png', // 替换为你的图片路径
            fit: BoxFit.cover,
            width: double.infinity,
            height: double.infinity,
          ),
          Padding(
            padding: const EdgeInsets.all(
              30 / ratio, // 像素值直接使用 dp 转换
            ),
            child: // 加载完成后显示商品信息
                Content01(child: Content()),
          ),
        ],
      ),
    );
  }
}

class Content01 extends StatelessWidget {
  final Widget child;

  const Content01({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return
    // ChangeNotifierProvider(
    // create: (_) => ShangpinkuProvider(),
    // child:
    Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TitleText(text: '商品库'),
        SizedBox(height: 0 / ratio),
        Row(
          spacing: 10,
          children: [TitleText(text: '我是筛选条件1'), TitleText(text: '我是筛选条件2')],
        ),
        Expanded(child: child),
        // child,
      ],
      // ),
    );
  }
}

class Content extends StatelessWidget {
  const Content({super.key});

  @override
  Widget build(BuildContext context) {
    final products = context.watch<ShangpinkuProvider>().products;
    return RobotCard(
      child: GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 6, // 每行 3 个网格
          crossAxisSpacing: 20 / ratio,
          mainAxisSpacing: 30 / ratio,
          childAspectRatio: 0.78,
        ),
        padding: const EdgeInsets.all(10.0),
        itemCount: products.length,
        itemBuilder: (context, index) {
          final product = products[index];
          return GestureDetector(
            onTap: () => {
              print('onTap'),
            Navigator.of(context).pushNamed(
            '/xiangqing',
            arguments: {
            'id': product.id,
            'name': product.name,
            },
            ),
            },
            child: ItemWidget(product),
          );
        },
      ),
    );
  }
}

class ItemWidget extends StatelessWidget {
  final Commodity product;

  const ItemWidget(this.product, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.cyan, // 背景颜色
        borderRadius: BorderRadius.circular(14 / ratio), // 圆角半径 14px
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(14.0/ratio),
            child:
          SizedBox(
            width: 284 / ratio,
            height: 284 / ratio,
            child: Image.network(
              product.thumbnail,
              fit: BoxFit.cover, // 图片会自动缩放并填满容器
            ),
          )),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                product.name.toString(),
                // textAlign: TextAlign.left, // 确保文本内容左对齐
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 24 / ratio,
                ),
              ),

              Text(
                product.price.toString(),
                // textAlign: TextAlign.left, // 确保文本内容左对齐
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 24 / ratio,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
